package com.meteor.user.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.meteor.framework.common.constant.Constant;
import com.meteor.framework.common.utils.PageResult;
import com.meteor.framework.common.utils.TreeUtils;
import com.meteor.framework.mybatis.service.impl.BaseServiceImpl;
import com.meteor.user.convert.SysMerchantConvert;
import com.meteor.user.entity.SysPost;
import com.meteor.user.mapper.SysMerchantMapper;
import com.meteor.user.entity.SysMerchant;
import com.meteor.user.query.SysMerchantQuery;
import com.meteor.user.query.SysPostQuery;
import com.meteor.user.service.SysMerchantService;
import com.meteor.user.vo.SysMerchantVO;
import com.meteor.user.vo.SysOrgVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 机构管理
 *
 * @author lynn
 * @date 2024/1/03
 */
@Service
@AllArgsConstructor
public class SysMerchantServiceImpl extends BaseServiceImpl<SysMerchantMapper, SysMerchant> implements SysMerchantService {

    private final SysOrgServiceImpl sysOrgService;

    @Override
    public PageResult<SysMerchantVO> page(SysMerchantQuery query) {

        if(!ObjectUtils.isEmpty(query.getOrgId())){
            query.setOrgIds(sysOrgService.getSubOrgIdList(query.getOrgId()));
        }
//        QueryWrapper<SysMerchant> queryWrapper = new QueryWrapper<>();
//        queryWrapper.like(ObjectUtil.isNotEmpty(query.getName()), "name", query.getName());

//        IPage<SysMerchant> page = baseMapper.selectPage(getPage(query), queryWrapper);

        IPage<SysMerchant> page = baseMapper.selectPage(getPage(query), getWrapper(query));

        return new PageResult<>(SysMerchantConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SysMerchantVO vo) {

        SysMerchant entity = SysMerchantConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysMerchantVO vo) {

        SysMerchant entity = SysMerchantConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String id) {

        removeById(id);
    }

    @Override
    public List<SysMerchantVO> allList() {

        List<SysMerchant> list = baseMapper.selectList(new QueryWrapper<SysMerchant>().eq("deleted", 0));

        return SysMerchantConvert.INSTANCE.convertList(list);
    }

    @Override
    public SysMerchantVO orgMerchant(String orgId) {

        // 商户列表
        SysMerchantVO entity = baseMapper.getOrgMerchant(orgId);

        return entity;
    }


    private Wrapper<SysMerchant> getWrapper(SysMerchantQuery query){
        LambdaQueryWrapper<SysMerchant> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(query.getName()), SysMerchant::getName, query.getName());
        wrapper.in(!ObjectUtils.isEmpty(query.getOrgIds()), SysMerchant::getOrgId, query.getOrgIds());

        // 数据权限
        dataScopeWrapper(wrapper);

        return wrapper;
    }
}
